package com.tmpt.controller.springmvc.handlers;

import com.tmpt.dto.A2UserDto;
import com.tmpt.dto.Result;
import com.tmpt.service.ISecurityV2Service;
import com.tmpt.utils.StringUtils;
import com.tmptSource.dto.*;
import com.tmptSource.service.intf.IA2userService;
import com.tmptSource.service.intf.IOwTaskService;
import com.tmptSource.service.intf.IOwTmpDisplayService;
import com.tmptSource.service.intf.IOwUserTrackService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by wxy on 2017/10/17.
 */

@Controller
@RequestMapping("/springmvc/owTmpAction")
public class OwTmpAction {

    private Logger log = Logger.getLogger(this.getClass());

    @Resource
    private IOwTmpDisplayService owTmpDisplayService;
    @Resource
    private ISecurityV2Service securityV2Service;
    @Resource
    private IOwUserTrackService owUserTrackService;
    @Resource
    private IOwTaskService owTaskService;
    @Resource
    private IA2userService a2userService;


    @RequestMapping(value = "/queryDisplayList")
    @ResponseBody
    public Result queryDisplayList( HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwTmpDisplayDto> result = new Result();
        try {
            //调用远程服务
            result =  owTmpDisplayService.pagination(new OwTmpDisplayDto(),null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("final+"+result.getContent());
        }
    }




    /**
     * 查看个人资料
     * @return
     */
    @RequestMapping(value = "/queryUserById")
    @ResponseBody
    public Result queryUserById(String userId ,HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<A2UserDto> result = new Result();
        try {
            //验证用户账号与密码是否对应
            A2UserDto empDto = securityV2Service.queryUserById(userId);
            if(empDto == null){
                return result.fault("用户名不存在");
            }
            if (empDto.getDeleteFlag() == 1){
                return result.fault("用户资料已删除");
            }

            empDto.setPassword("");
            empDto.setAppPassword("");

            result.setResultObj(empDto);
            return result.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("loginApp final："+result.getContent());
        }
    }


    /**
     * 查看个人资料
     * @return
     */
    @RequestMapping(value = "/queryUsersFormat")
    @ResponseBody
    public Result queryUsersFormat(HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<A2userDto> result = new Result();
        try {
            PageDto pageDto = new PageDto();
            pageDto.setPageSize(1000000000);
            pageDto.setPageCurrent(0);
            pageDto.setPageIndex(0);
            pageDto.setSortColumn("id");
            pageDto.setSortOrder("desc");

            //验证用户账号与密码是否对应
            result = a2userService.pagination(new A2userDto(), pageDto);
            if(!result.isSuccess()){
                return result;
            }
            Map<String,A2userDto> mapOut = new HashMap<String, A2userDto>();
            for(A2userDto a2userDto: (List<A2userDto>)result.getResultMap().get("rows")){
                String id = a2userDto.getId();
                mapOut.put(id,a2userDto);
            }
            result.getResultMap().put("rows",mapOut);


            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("loginApp final："+result.getContent());
        }
    }

    @RequestMapping(value = "/queryUserTrackList")
    @ResponseBody
    public Result queryUserTrackList(OwUserTrackDto owUserTrackDto, String sortOrder,  HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwUserTrackDto> result = new Result();
        try {
            //查询参数
            PageDto pageDto = new PageDto();
            pageDto.setPageSize(1000000000);
            pageDto.setPageCurrent(0);
            pageDto.setPageIndex(0);
            pageDto.setSortColumn("id");
            pageDto.setSortOrder("desc");
            if(!StringUtils.isNull(sortOrder)) {
                pageDto.setSortOrder(sortOrder);
            }
            //调用远程服务
            Result<OwUserTrackDto> result1 =  owUserTrackService.pagination(owUserTrackDto, pageDto);
            if(!result1.isSuccess()){
                return result1;
            }
            List<Map<String,String>> rowsList = new ArrayList<Map<String, String>>();
            Map<String,String> map = null;
            for(OwUserTrackDto owUserTrackDto1: (List<OwUserTrackDto>)result1.getResultMap().get("rows")){
                map = new HashMap<String, String>();
                map.put("lat",owUserTrackDto1.getLat());
                map.put("lng",owUserTrackDto1.getLng());
                map.put("a2userId",owUserTrackDto1.getA2userId());
                map.put("createdate",owUserTrackDto1.getCreatedate());
                map.put("id",owUserTrackDto1.getId());
                rowsList.add(map);
            }
            result.getResultMap().put("rows",rowsList);

            return result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("final+"+result.getContent());
        }
    }



    /**
     * 添加数据:地图管理中生成任务
     * @return
     */
    @RequestMapping(value = "/addData4genTask")
    @ResponseBody
    public Result addData4genTask(OwTaskDto owTaskDto, HttpServletRequest request){
        Result<OwTaskDto> resultObj = new Result();
        try {

            //插入参数
            //OwTaskDto owTaskDto = new OwTaskDto();
            //String adminMan = WebUtils.getUsername();//加入管理员
            //String userId = WebUtils.getUserId();
            //owTaskDto.setCreatemanId("1");
            //owTaskDto.setCreateman("tmp_admin");
            //owTaskDto.setCompanyId("1");
            /*
            //插入数据
            owTaskDto.setRegisterDataNumber(this.registerDataNumber);
            owTaskDto.setTaskNumber(this.taskNumber);
            owTaskDto.setName(this.name);
            owTaskDto.setOwPriorityCode(this.owPriorityCode);
            owTaskDto.setOwTaskType(this.owTaskType);
            owTaskDto.setOwRegisterType(this.owRegisterType);
            owTaskDto.setOwViolationType(this.owViolationType);
            owTaskDto.setFinderDeptId(this.finderDeptId);
            owTaskDto.setFinderDeptName(this.finderDeptName);
            owTaskDto.setBaseCommiteeId(this.baseCommiteeId);
            owTaskDto.setBaseRoadId(this.baseRoadId);
            owTaskDto.setPosHouseNumber(this.posHouseNumber);
            owTaskDto.setPosDesc(this.posDesc);
            owTaskDto.setPosLng(this.posLng);
            owTaskDto.setPosLat(this.posLat);
            owTaskDto.setLitigantName(this.litigantName);
            owTaskDto.setLitigantPhone(this.litigantPhone);
            owTaskDto.setMainManId(this.mainManId);
            owTaskDto.setAssistManId(this.assistManId);
            owTaskDto.setMainManName(this.mainManName);
            owTaskDto.setAssistManName(this.assistManName);
            owTaskDto.setIssueContent(this.issueContent);
            owTaskDto.setRemark(this.remark);
            owTaskDto.setImproveRequied(this.improveRequied);
            owTaskDto.setDealIssueContent(this.dealIssueContent);
            owTaskDto.setOwDealType(this.owDealType);
            owTaskDto.setDealTime(this.dealTime);
            owTaskDto.setDealResult(this.dealResult);
            owTaskDto.setDealRemark(this.dealRemark);
            */
            //添加图片
            /*String[] owPictureMagDtoArr = this.getOwPictureMagDtoListStr().split(";");
            for(int i=0;i<owPictureMagDtoArr.length;i++){
                String imgPath = owPictureMagDtoArr[i];
                OwPictureMagDto owPictureMagDto = new OwPictureMagDto();
                owPictureMagDto.setPath(imgPath);
                owTaskDto.getOwPictureMagDtoList().add(owPictureMagDto);
            }*/
            //check: 主办和协办不能重复同一个人
            /*if(this.assistManId!=null) {
                List<String> mainManLdList = Arrays.asList(this.mainManId.split(", "));
                List<String> assistManIdList = Arrays.asList(this.assistManId.split(", "));
                for (String assistManIdItem : assistManIdList) {
                    if (mainManLdList.contains(assistManIdItem)) {
                        resultObj.fault("主办和协办不能重复同一个人");
                        this.jsonArrayResult.add(resultObj.getResultMap());
                        return "jsonArray";
                    }
                }
            }*/

            //调用远程服务
            Map<String,Object> paramMap = new HashMap<String, Object>();
            //paramMap.put("owIssueDeclareId", owIssueDeclareId);//待办问题ID,伪删除掉
            resultObj =  owTaskService.addDataAndAssign(owTaskDto,paramMap);
            /*if(!resultObj.isSuccess()){
                this.result = "error:"+resultObj.getContent();
                JsonPaginationUtils.responseWriter(response, this.result);
                return "jsonResult";
            }
            this.jsonArrayResult.add(resultObj.getResultMap());*/

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
            //this.jsonArrayResult.add(resultObj.getResultMap());
        }
        return resultObj;
    }




    @RequestMapping(value = "/queryClaimTaskNumber")
    @ResponseBody
    public Result queryClaimTaskNumber(long owTaskId, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwUserTrackDto> result = new Result();
        try {
            //调用远程服务
            result =  owTaskService.queryClaimTaskNumber(owTaskId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("final+"+result.getContent());
        }
    }



    @RequestMapping(value = "/updateOwTmpDisplayPeopleNum")
    @ResponseBody
    public Result updateOwTmpDisplayPeopleNum(OwTmpDisplayDto owTmpDisplayDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwTmpDisplayDto> result = new Result();
        try {
            //调用远程服务
            result =  owTmpDisplayService.editData_PeopleNum(owTmpDisplayDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
            //System.out.println("final+"+result.getContent());
        }
    }






}